package com.cupk.repository;

import com.cupk.entity.Admin;
import com.cupk.entity.User;
import com.cupk.util.JDBCTools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author LY
 * @version 1.0
 */
public class AdminRepository {
    public AdminRepository(){}

    //管理员登录
    public Admin login(String username, String password){
        Connection connection = JDBCTools.getConnection();
        String sql = "select * from adminlist where username = ? and password = ?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Admin admin = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, password);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                admin = new Admin(resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, statement, resultSet);
        }
        return admin;
    }

    //删除电影或用户
    public void deleteMU(Integer id, String type){
        Connection connection = JDBCTools.getConnection();
        String sql = null;
        if(type.equals("movie")){
            sql = "DELETE FROM movieList WHERE id = ?;";
        }else{
            sql = "DELETE FROM userlist WHERE id = ?;";
        }
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            int result = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, null);
        }
    }
}
